﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aribaccio;
using BDPAutodetect.Device;
using CommunityToolkit.Mvvm.ComponentModel;

namespace BDPAutodetect.Models
{
    /// <summary>
    /// 工位指标检测数据
    /// </summary>
    public partial class MTargetDetectDto : ObservableModel
    {
        /// <summary>
        /// 指标
        /// </summary>
        [ObservableProperty]
        protected MTarget target;

        /// <summary>
        /// 检测状态
        /// </summary>
        [ObservableProperty]
        protected SwitchType detectSwitch;

        /// <summary>
        /// 检测颜色
        /// </summary>
        [ObservableProperty]
        protected Color detectColor;

        /// <summary>
        /// 检测结果结果
        /// </summary>
        [ObservableProperty]
        protected string detectResult;

        /// <summary>
        /// 检测结果结果值
        /// </summary>
        [ObservableProperty]
        protected string detectResultValue;

        /// <summary>
        /// 检测结果接收状态
        /// </summary>
        [ObservableProperty]
        protected ReceiveStatus detectResultStatus;

        /// <summary>
        /// 检测结果状态颜色
        /// </summary>
        [ObservableProperty]
        protected Color detectResultColor;

        /// <summary>
        /// 检测状态
        /// </summary>
        [ObservableProperty]
        protected DetectJobStatus detectStatus;

        /// <summary>
        /// 参数界限判断标准
        /// </summary>
        [ObservableProperty]
        protected MParameterLimitData? parameterLimit;

        /// <summary>
        /// 指标类型
        /// </summary>
        public DeviceTargetType TargetType => this.Target?.TargetType ?? DeviceTargetType.None;

        /// <summary>
        /// 检测结果是否合格状态
        /// </summary>
        public DetectResultStatus DetectResultPassStatus => this.DetectResultStatus == ReceiveStatus.None || this.DetectResultStatus == ReceiveStatus.OK ? Models.DetectResultStatus.Pass : Models.DetectResultStatus.Fail;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="argTarget">指标</param>
        public MTargetDetectDto(MTarget argTarget)
        {
            this.Target = argTarget;
            this.DetectSwitch = SwitchType.Off;
            this.DetectColor = Color.White;
            this.DetectResult = string.Empty;
            this.DetectResultValue = string.Empty;
            this.DetectResultStatus = ReceiveStatus.None;
            this.DetectResultColor = Color.White;
            this.DetectStatus = DetectJobStatus.None;
        }

        /// <summary>
        /// 设置检测状态
        /// </summary>
        /// <param name="argDetectSwitch"></param>
        public void SetDetectSwitch(SwitchType argDetectSwitch)
        {
            this.DetectSwitch = argDetectSwitch;
            switch (this.DetectSwitch)
            {
                case SwitchType.On:
                    this.DetectColor = Color.Yellow;
                    break;
                case SwitchType.Off:
                default:
                    this.DetectColor = Color.White;
                    break;
            }
        }

        /// <summary>
        /// 设置检测结果状态
        /// </summary>
        public void SetDetectResultStatus(string argDetectResult, string argDetectResultValue, ReceiveStatus argDetectResultStatus)
        {
            this.DetectResult = argDetectResult;
            this.DetectResultValue = argDetectResultValue;
            this.DetectResultStatus = argDetectResultStatus;
            switch (this.DetectResultStatus)
            {
                case ReceiveStatus.OK:
                    this.DetectResultColor = Color.GreenYellow;
                    break;
                case ReceiveStatus.NC:
                    this.DetectResultColor = Color.OrangeRed;
                    break;
                case ReceiveStatus.NG:
                    this.DetectResultColor = Color.Red;
                    break;
                case ReceiveStatus.None:
                default:
                    this.DetectResultColor = Color.White;
                    break;
            }
        }

        /// <summary>
        /// 重置
        /// </summary>
        public void Reset()
        {
            this.DetectSwitch = SwitchType.Off;
            this.DetectColor = Color.White;
            this.DetectResult = string.Empty;
            this.DetectResultStatus = ReceiveStatus.None;
            this.DetectResultColor = Color.White;
            this.DetectStatus = DetectJobStatus.None;
            this.ParameterLimit = null;
        }

        /// <summary>
        /// 转换数据
        /// </summary>
        /// <returns></returns>
        public decimal ToDataNumber()
        {
            if (decimal.TryParse(this.DetectResultValue, out decimal dResultValue))
            {
                return dResultValue;
            }
            return 0;
        }

        /// <summary>
        /// 深度克隆
        /// </summary>
        /// <returns></returns>
        public MTargetDetectDto CloneDeep()
        {
            return new MTargetDetectDto(this.Target)
            {
                DetectSwitch = this.DetectSwitch,
                DetectColor = this.DetectColor,
                DetectResult = this.DetectResult,
                DetectResultValue = this.DetectResultValue,
                DetectResultStatus = this.DetectResultStatus,
                DetectResultColor = this.DetectResultColor,
                DetectStatus = this.DetectStatus,
                ParameterLimit = this.ParameterLimit?.CloneDeep()
            };
        }
    }
}
